from setting import *
from enemyplane import *


class EnemyUnit(pygame.sprite.Sprite):
    def __init__(self):
        self._layer = 2
        self.groups = allgroup, enemygroup
        pygame.sprite.Sprite.__init__(self, self.groups)
        self.image = pygame.image.load('./images/enemy/enemy0.png')
        self.rect = self.image.get_rect()
        #计数器
        self.count = 0
        #设置飞机出现间隔用
        self.interval = 1000
        self.start_time = pygame.time.get_ticks()
        #用了sprite必须有个image ,用了个投机的办法，放到屏幕外面去了。
        self.set_pos(-300,-300)
        #控制飞行轨迹的点
        self.points = [(-10, -10), (200, 800), (300, 200), (1000, 200)]

    def set_pos(self,x,y):
        self.rect.x = x
        self.rect.y = y

    def update(self):
        cur_time = pygame.time.get_ticks()
        if cur_time - self.start_time < self.interval:
            return
        self.start_time = cur_time
        enemy = EnemyPlane()
        enemy.set_bezier_path(self.points)
        enemy.set_pos(-10,-10)
        self.count  +=1
        #大于7架，就退出了
        if self.count > 6:
            self.kill()



class BEnemyUnit(pygame.sprite.Sprite):
    def __init__(self):
        self._layer = 2
        self.groups = allgroup, enemygroup
        pygame.sprite.Sprite.__init__(self, self.groups)
        self.image = pygame.image.load('./images/enemy/enemy0.png')
        self.rect = self.image.get_rect()
        #计数器
        self.count = 0
        #设置飞机出现间隔用
        self.interval = 1000
        self.start_time = pygame.time.get_ticks()
        #用了sprite必须有个image ,用了个投机的办法，放到屏幕外面去了。
        self.set_pos(-300,-300)
        #控制飞行轨迹的点
        self.points = [(400, -20), (200, 200), (400, 400), (600, 600),(400,800),(200,600),(400,400),(600,200),(200,-200)]

    def set_pos(self,x,y):
        self.rect.x = x
        self.rect.y = y

    def update(self):
        cur_time = pygame.time.get_ticks()
        if cur_time - self.start_time < self.interval:
            return
        self.start_time = cur_time
        enemy = EnemyPlane()
        enemy.set_BSpline_path(self.points)
        enemy.set_pos(400,-20)
        self.count  +=1
        #大于16架，就退出了
        if self.count > 15:
            self.kill()